package com.anjie.powerproject.controller.test;

import com.anjie.powerproject.common.FillStrategyEnum;
import com.anjie.powerproject.common.Result;
import com.anjie.powerproject.service.impl.DataLoader;
import com.anjie.powerproject.strategy.utils.DateUtils;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.apache.catalina.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("xy")
public class XyTestController {

    @Autowired
    ExpressRunner runner;

    @Autowired
    DataLoader dataLoader;

    @PostMapping("/express")
    @ResponseBody
    public Result<Object> testExpress(@RequestParam("script") String script) {
        try {
            Object execute = runner.execute(script, new DefaultContext<>(), new ArrayList<>(), false, true);
            return Result.ok(execute);
        } catch (Exception ex) {
            return Result.error(ex.getMessage());
        }
    }

    @GetMapping("/root")
    @ResponseBody
    public String getRootDir() throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        return path;
    }

    @GetMapping("/test")
    @ResponseBody
    public Result<DataLoader.DataSet> test(HttpServletRequest request) {
        try {
            String base = request.getServletContext().getRealPath("WEB-INF/classes/");
            String bases[] = new String[]{base, "src/main/resources/"};
            Date startTime = DateUtils.parseStringToDate(DateUtils.ymdFormat, "2021-09-01");
            Date endTime = DateUtils.parseStringToDate(DateUtils.ymdFormat, "2021-09-30");
            DataLoader.DataSet dataSet = dataLoader.buildData(bases, startTime, endTime, "yc1008", FillStrategyEnum.FILL_OTHER_VALUE, 123.0);
            return Result.ok(dataSet);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
